package com.utils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class util {

    public static void getTimeWheel(LocalDateTime localDateTime, Runnable runnable) {


        LocalDateTime now = LocalDateTime.now();

        System.out.println("预计执行时间: " + util.LocalDateTimeToString(localDateTime));
        System.out.println("当前时间: " + util.LocalDateTimeToString(now));

        Duration between = Duration.between(now, localDateTime);
        long toMillis = between.toMillis();

        Timer timer = new Timer();
        timer.addTask(new TimerTask(toMillis, runnable));

    }

    /**
     * LocalDateTime 转 yyyy-MM-dd HH:mm:ss
     *
     * @param localDateTime
     * @return
     */
    public static String LocalDateTimeToString(LocalDateTime localDateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(localDateTime);
        return format;
    }

    /**
     * 毫秒 转 LocalDateTime
     *
     * @param millis
     * @return
     */
    public static LocalDateTime millisToLocalDateTime(long millis) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(millis / 1000, 0, ZoneOffset.ofHours(8));
        return localDateTime;
    }

}
